<?php

class M_user extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	function save($username, $nama, $password, $serial, $newsletter){
		$data=array(
			"user_id"=>$username,
			"user_nama"=>$nama,
			"user_password"=>$password,
			"user_tsn"=>$serial,
			"user_newsletter"=>$newsletter,
			"user_register"=>date('Y-m-d H:i:s'),
			"user_confirm"=>$this->m_public_function->enkrip("TAKITA",$username.session_id())
		);
		
		$rs=$this->db->insert('takita_user',$data);
		if($this->db->affected_rows()){
			$sql="UPDATE takita_register SET register_produk='Y'
					WHERE register_serial='".$serial."'";
			$this->db->query($sql);
			
			return 1;
		}else{
			return 0;
		}
	}

	
	function check_data($username){
		$query = $this->db->query("SELECT * FROM M_USERS WHERE USER_NAME = '".$username."'");
		if ($query->num_rows() == 0){
			return 1;
		}else{
			return 0;
		}
	}
	
	function get_user(){
		$sql="SELECT profil_avatar FROM takita_profil ORDER BY RAND() LIMIT 10";
		$rs=$this->db->query($sql);
		
		if ($rs->num_rows()){
			return $rs->result();
		}else{
			return 0;
		}
	}
	function cek_serial($serial){
		$sql="SELECT register_serial, register_produk FROM takita_register
				WHERE register_serial='".$serial."'";
		$query = $this->db->query($sql);
		if ($query->num_rows()){
			$row=$query->row();
			if($row->register_produk=='Y'){
				
				
				return 1;
			}else{
				return 0;
			}
			
		}else{
			return -1;
		}
	}
	
	function cek_profile(){
		$sql="SELECT profil_userid FROM takita_profil
				WHERE md5(profil_userid)='".$_SESSION[SESSION_USERID]."'";
		$query = $this->db->query($sql);
		if ($query->num_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	
	function save_profil($username,
							$nama,
							$kelamin,
							$alamat,
							$kota,
							$propinsi,
							$tanggal_lahir,
							$facebook,
							$twitter,
							$website,
							$bio){
		
		$sql="SELECT profil_userid FROM takita_profil
				WHERE profil_userid='".$username."'";
		$rs=$this->db->query($sql);
		
		if($rs->num_rows()){
			
			$sql="UPDATE takita_profil SET
					profil_kelamin='$kelamin',
					profil_kota='$kota',
					profil_alamat='$alamat',
					profil_tgllahir='$tanggal_lahir',
					profil_propinsi='$propinsi',
					profil_facebook='$facebook',
					profil_twitter='$twitter',
					profil_website='$website',
					profil_bio='$bio'
				WHERE	profil_userid='".$username."'";
			$result=$this->db->query($sql);
			
		}else{
			
			$sql="INSERT into takita_profil(profil_userid,
										profil_kelamin,
										profil_alamat,
										profil_kota,
										profil_propinsi,
										profil_tgllahir,
										profil_facebook,
										profil_twitter,
										profil_website,
										profil_bio)
							VALUES('$username',
									'$kelamin',
									'$alamat',
									'$kota',
									'$propinsi',
									'$tanggal_lahir',
									'$facebook',
									'$twitter',
									'$website',
									'$bio')";
			
			$result=$this->db->query($sql);
									
		}
		
		return 1;
		
	}
	function hapus($id){
		$this->db->where('USER_NAME',$id);
		$this->db->delete('M_USERS');
		echo 1;
	}

	
	
}